package com.cckat.controller;

import com.cckat.entity.UserInfo;
import com.cckat.entity.bo.LoginBo;
import com.cckat.entity.bo.RegisterBo;
import com.cckat.result.Result;
import com.cckat.result.ResultCodeEnum;
import com.cckat.service.UserInfoService;
import com.cckat.utils.MD5Util;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 静海基地
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable String phone, HttpSession session){
        String code = "1234";
        //将验证码共享到session中
        session.setAttribute("CODE", code);
        return Result.ok();
    }

    @RequestMapping("/register")
    public Result register(@RequestBody RegisterBo registerBo, HttpSession session){
        //获取session中存储的正确的验证码
        String codeInSession = (String) session.getAttribute("CODE");
        //验证验证码
        if(!registerBo.getCode().equals(codeInSession)){
            //验证码错误
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //根据手机号查询用户信息
        UserInfo userInfo = userInfoService.findUserInfoByPhone(registerBo.getPhone());
        //判断手机号是否已经被注册
        if(userInfo != null){
            //表示手机号已经被注册
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
        //注册
        userInfo = new UserInfo();
        BeanUtils.copyProperties(registerBo, userInfo);
        userInfo.setStatus(1);
        userInfo.setPassword(MD5Util.encrypt(userInfo.getPassword()));
        userInfoService.insert(userInfo);
        return Result.ok();
    }


    @RequestMapping("/login")
    public Result login(@RequestBody LoginBo loginBo, HttpSession session){
        //判断手机号是否正确（即手机号在user_info中是否存在）
        UserInfo userInfo = userInfoService.findUserInfoByPhone(loginBo.getPhone());
        if(userInfo == null){
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //在手机号正确的情况下，判断密码是否正确
        if(!MD5Util.encrypt(loginBo.getPassword()).equals(userInfo.getPassword())){
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //在手机号和密码都正确的情况下，判断该用户是否已经被锁定
        if(userInfo.getStatus() == 0){
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        session.setAttribute("USER", userInfo);
        //表示登录成功
        Map<String, Object> map = new HashMap<>();
        map.put("nickName", userInfo.getNickName());
        map.put("phone", userInfo.getPhone());
        return Result.ok(map);
    }

    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.ok();
    }

}
